package sk.atletis.atletika;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.json.JSONArray;
import android.content.ContentValues;

public class JsonParser 
{
	private JSONArray json;
	
	public JsonParser(JSONArray json)
	{
		this.json = json;
	}
	
	public ContentValues[] parseCompetitions()
	{
		try
		{
			List<ContentValues> myCompetitions = new ArrayList<ContentValues>();
			for(int i = 0; i < json.length(); i++)
			{
				
				if(json.getJSONObject(i).getBoolean("bResults"))
				{
					ContentValues newCompetition = new ContentValues();
					newCompetition.put(CompetitionsContract._ID, json.getJSONObject(i).getInt("ID"));
					newCompetition.put(CompetitionsContract.NAME, json.getJSONObject(i).getString("Name"));
					newCompetition.put(CompetitionsContract.CITY, json.getJSONObject(i).getString("City"));
					newCompetition.put(CompetitionsContract.RESULTS_PATH, json.getJSONObject(i).getString("sResults"));
					
					// DateFrom
					String s = json.getJSONObject(i).getString("DateFrom");
					Date d = new Date(Long.parseLong(s.substring(6, s.length() - 2)));
					Locale l = Locale.getDefault();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", l);
					s = sdf.format(d);
					newCompetition.put(CompetitionsContract.DATE_FROM, sdf.format(d));
					
					myCompetitions.add(newCompetition);
				}
			}
			
			return myCompetitions.toArray(new ContentValues[0]);
			
		}
		catch(Exception e) { }
		
		return null;
	}
	
	// Parses JSONArray to ContentValues[]
	public ContentValues[] parseArticles() 
	{
		try	
		{
			List<ContentValues> myArticles = new ArrayList<ContentValues>();
			
			for(int i = 0; i < json.length(); i++)
			{
				ContentValues newArticle = new ContentValues();
				newArticle.put(ArticlesContract._ID, json.getJSONObject(i).getInt("ID"));
				newArticle.put(ArticlesContract.CATEGORYID, json.getJSONObject(i).getInt("CategoryId"));
				newArticle.put(ArticlesContract.TITLE, json.getJSONObject(i).getString("Title"));
				newArticle.put(ArticlesContract.CITY, json.getJSONObject(i).getString("City"));
				newArticle.put(ArticlesContract.EDITOR, json.getJSONObject(i).getString("Editor"));
				newArticle.put(ArticlesContract.PEREX, json.getJSONObject(i).getString("Perex"));
				newArticle.put(ArticlesContract.ARTICLETEXT, json.getJSONObject(i).getString("Text"));
				newArticle.put(ArticlesContract.IMAGEPATH, json.getJSONObject(i).getString("ImagePath"));
										
				// Date
				String s = json.getJSONObject(i).getString("Date");
				Date d = new Date(Long.parseLong(s.substring(6, s.length() - 2)));
				Locale l = Locale.getDefault();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", l);
				s = sdf.format(d);
				newArticle.put(ArticlesContract.DATE, sdf.format(d));
			
				myArticles.add(newArticle);
			}
			
			if (!myArticles.isEmpty())
			{
				return myArticles.toArray(new ContentValues[0]);
			}
		}
		catch(Exception e) { }
		
		return null;
	}
	
	
	
	
	
}
